from django.contrib.auth import get_user_model
from rest_framework import serializers

from .models import Question

User = get_user_model()


class QuestionChangeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Question
        fields = '__all__'


class QuestionGetSerializer(serializers.ModelSerializer):
    asked_by = serializers.SerializerMethodField()
    rejected_by = serializers.SerializerMethodField()
    audited_by = serializers.SerializerMethodField()
    answered_by = serializers.SerializerMethodField()

    class Meta:
        model = Question
        fields = '__all__'

    @staticmethod
    def get_user_info(user):
        if not user:
            return None

        return {
            'id': user.id,
            'username': user.username,
            'groupname': user.groups.first().name
        }

    def get_asked_by(self, obj):
        return self.get_user_info(obj.asked_by)

    def get_rejected_by(self, obj):
        return self.get_user_info(obj.rejected_by)

    def get_audited_by(self, obj):
        return self.get_user_info(obj.audited_by)

    def get_answered_by(self, obj):
        return self.get_user_info(obj.answered_by)
